package com.sinosoft.brokerchannel.base.server;

import com.alibaba.fastjson.JSONObject;
import com.sinosoft.brokerchannel.base.common.SystemParamFinal;
import com.sinosoft.brokerchannel.base.utile.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 日志输出类
 * 程序中调用其他系统地方日志输出，统一使用日志输出服务，如果后续不输出或者改用其他方式只需要调整这一个类就可以
 */

@Service
public class LogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 对象输出
     *
     * @param instruction
     * @param o
     */
    public void objectPrint(String instruction, Object o) {
        logger.info(SystemParamFinal.LINE_SEPARATOR + "----------------------------------------" + instruction + "----------------------------------------" + SystemParamFinal.LINE_SEPARATOR + CommonUtil.dto2xml(o) + SystemParamFinal.LINE_SEPARATOR + "----------------------------------------" + instruction + "----------------------------------------" + SystemParamFinal.LINE_SEPARATOR);
    }

    /**
     * 字符串输出
     *
     * @param instruction
     * @param str
     */
    public void stringPrint(String instruction, String str) {
        logger.info(SystemParamFinal.LINE_SEPARATOR + "----------------------------------------" + instruction + "----------------------------------------" + SystemParamFinal.LINE_SEPARATOR + str + SystemParamFinal.LINE_SEPARATOR + "----------------------------------------" + instruction + "----------------------------------------" + SystemParamFinal.LINE_SEPARATOR);
    }


    /**
     * 打印调用服务耗时
     * @param arg 类对象
     * @param method 调用服务的方法名
     * @param firstTime	耗时
     * @return
     */

    public void timeConsuming(String key,Object arg, String method, long firstTime) {
        JSONObject timeVo = new JSONObject();

        timeVo.put("key", key);
        timeVo.put("class", arg.getClass().getName());
        timeVo.put("method", method);
        timeVo.put("description", "Invokes the service time record.");
        long time = System.currentTimeMillis()-firstTime;
        timeVo.put("time(ms)", time);

        logger.info(timeVo.toString());
    }
}
